package com.fr.report.script.function;

import com.fr.base.FRContext;
import com.fr.base.file.DatasourceManager;
import com.fr.data.Dictionary;
import com.fr.data.TableData;
import com.fr.data.impl.TableDataDictionary;
import com.fr.report.TableDataSource;
import com.fr.report.script.Calculator;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.util.Utils;

public class MAP extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 2)
      return Primitive.ERROR_NAME;
    Object localObject1 = null;
    TableData localTableData = null;
    int i = 0;
    int j = 0;
    Object localObject2 = paramArrayOfObject[0];
    String str = Utils.objectToString(paramArrayOfObject[1]);
    if (paramArrayOfObject.length > 2)
      i = Utils.objectToNumber(paramArrayOfObject[2], false).intValue() - 1;
    if (paramArrayOfObject.length > 3)
      j = Utils.objectToNumber(paramArrayOfObject[3], false).intValue() - 1;
    TableDataSource localTableDataSource = getCalculator().getCurrentTableDataSource();
    if (localTableDataSource != null)
      localTableData = localTableDataSource.getTableData(str);
    if (localTableData == null)
    {
      localObject3 = FRContext.getDatasourceManager();
      localTableData = ((DatasourceManager)localObject3).getTableData(str);
    }
    if (localTableData == null)
      localObject1 = Dictionary.EMPTY_DICT;
    else
      localObject1 = new TableDataDictionary(localTableData, i, j);
    Object localObject3 = ((Dictionary)localObject1).get(localObject2, getCalculator());
    return ((localObject3 != null) ? localObject3 : localObject2);
  }

  public Function.Type getType()
  {
    return Function.REPORT;
  }

  public String getEN()
  {
    return "MAP(object, string, int, int):four arguments are keyValue, name of TableData, keyIndex and valueIndex.\nExample:\nMAP(1001, \"employee\", 1, 2) find the record int TableData:employee where the value in first column is key, and return this value in the second column of this record.";
  }

  public String getCN()
  {
    return "MAP(object, string, int, int):四个参数分别是索引值,数据集的名字,索引值所在列序号,返回值所在列序号。\n根据数据集的名字,找到对应的数据集,找到其中索引列的值为key所对应的返回值。\n数据集的查找方式是依次从报表数据集找到服务器数据集。\n索引列序号与返回值序列号的初始值为1示例:\nMAP(1001, \"employee\", 1, 2)返回employee数据集,第1列中值为1001那条记录中第2列的值";
  }
}